<template>
	<view>
		<view class="">
			<!-- 2.0.19支持autoBack，默认为false -->
			<u-navbar leftText="看房评价" :autoBack="true">
			</u-navbar>
		</view>
		<view class="con-body">
			<view class="com_head">
				<image mode="widthFix" src="https://resourse.cnlhjt.com/upload/20221219/f72ab5e051f6892bdf046fba831a8d79.png"></image>
				<view class="dis_inl_blo com_head_right">
					<text>房屋评分</text>
					<view style="font-size: 22rpx;margin-top:15rpx;">
						
						<view class="dis_inl_blo">
							<u-rate :current="5" v-model="star" activeColor="#fc0" size="18"></u-rate>
						</view>
						
						<text style="margin-left:20rpx;">{{star}}分</text>
					</view>
				</view>
			</view>
			<view>
				<view class="con_con">
					<textarea name="" v-model="content" id="" cols="30" rows="5" placeholder="请留下您的意见"></textarea>
					<view style="margin-top: 20rpx;">
						<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
							:maxCount="9"></u-upload>
					</view>
				</view>
			</view>
			<u-toast ref="uToast"></u-toast>
			<view class="btn_con" @click="tocomment">提交评论</view>
		</view>
	</view>
</template>

<script>
	import {
		commentRent
	} from "@/service/rentService.js";
	export default {
		data () {
			return {
				id:0,
				star:5,
				fileList1: [],
				images: [],
				content:'',
				isselect:false,
				selectvalue:0,
			}
		},
		onLoad(options) {
			this.id=options.id
		},
		methods:{
			
			toselect(){
				this.isselect=!this.isselect;
				if(this.isselect){
					this.selectvalue=1
				}else{
					this.selectvalue=0
				}
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
				this.images.splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://admin.gslhzhwy.com/api/upload/file', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'smart-property'
						},
						success: (res) => {
							let par = JSON.parse(res.data);
							if (par.code == 1) {
								this.images.push(par.data.url);
							}
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			tocomment(){
				var obj={};
				obj.gid=this.id;
				obj.content=this.content;
				obj.images=this.images;
				obj.star=this.star;
				commentRent(obj).then(res=>{
					if(res.code==200){
						this.$refs.uToast.show({
							type: 'success',
							message: "提交成功",
							complete() {
								let pages = getCurrentPages();
								let beforePage = pages[pages.length - 2];
								beforePage.$vm.reFresh = Math.random()
								uni.navigateBack()
							}
						})
					}else{
						this.$refs.uToast.show({
							type: 'error',
							message: res.msg,
							icon:false
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #f8f8f8;
	}
	.com_head{
		margin:20rpx 0;
		background-color: #fff;padding:40rpx;
		image{
			width:80rpx;
			border-radius: 15rpx;
		}
		.com_head_right{
			margin-left:25rpx;
			font-size: 30rpx;
			color:#333;
		}
	}
	.con_con{
		padding:40rpx;
		border-bottom:2rpx solid #f8f8f8;
		background-color: #fff;
		textarea{
			width:94%;
			height:160rpx;
			background-color: #f8f8f8;
			border-radius: 15rpx;
			padding:15rpx;
			font-size: 28rpx;
		}
	}
	.btn_con{
		position: fixed;
		bottom:40rpx;
		width:94%;
		left:3%;
		height:70rpx;
		color:#fff;
		text-align: center;
		line-height: 70rpx;
		border-radius: 35rpx;
		background-image: linear-gradient(to right,#008189,#0c515c);
	}
</style>